package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.ThreadData;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 宋
 * @since 2022-06-06
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 清空购物车
     * Request URL: http://127.0.0.1:8080/shoppingCart/clean
     * Request Method: DELETE
     */
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session) {

        Long id = ThreadData.getId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, id);
        shoppingCartService.remove(queryWrapper);


        return R.success("清除成功");
    }


    /**
     * 用户登录后 购物车 的数据回显
     * Request URL: http://127.0.0.1:8080/shoppingCart/list
     * Request Method: GET
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session) {
        //查找登录用户的购物车 用登录用户的id
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        // TODO
        return R.success(list);
    }


    /**
     * Request URL: http://127.0.0.1:8888/shoppingCart/add
     * Request Method: POST
     * <p>
     * {amount: 40, setmealId: "1415580119015145474", name: "儿童套餐A计划",…}
     * amount: 40
     * image: "61d20592-b37f-4d72-a864-07ad5bb8f3bb.jpg"
     * name: "儿童套餐A计划"
     * setmealId: "1415580119015145474"
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        // 注意: 绑定添加购物车的用户id
        Long userId = (Long) session.getAttribute("user");

        shoppingCart.setUserId(userId);
        //数据相同的情况下 在数据表里更新数量 而不是继续添加,只有第一次才是添加操作
        // 1 . 先判断添加的是套餐还是菜品 再判断数据库里是否已经存在

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        Long dishId = shoppingCart.getDishId();

        if (dishId == null) {
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        }

        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
        if (shoppingCart1 == null) {
            // 不存在 新增
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart1 = shoppingCart;
            shoppingCartService.save(shoppingCart1);
        } else {
            //存在 更新
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
             shoppingCartService.updateById(shoppingCart1);
        }
        return R.success(shoppingCart1);
    }
}

